<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/04/13 00:16:19 $)
// $RCSfile: functions_chathistory.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Get the departments assigned to this staff
*/
function getChatDepartments()
{
	global $dbCore, $_SWIFT;

	$departmentidlist = array();
	$dbCore->query("SELECT departments.departmentid, departments.title, departments.departmentmodule FROM `". TABLE_PREFIX ."departments` AS departments LEFT JOIN `". TABLE_PREFIX ."staffassigns` AS staffassigns ON (staffassigns.departmentid = departments.departmentid) LEFT JOIN `". TABLE_PREFIX ."groupassigns` AS groupassigns ON (groupassigns.departmentid = departments.departmentid) WHERE staffassigns.staffid = '". intval($_SWIFT["staff"]["staffid"]) ."' OR groupassigns.staffgroupid = '". intval($_SWIFT["staff"]["staffgroupid"]) ."';");
	while ($dbCore->nextRecord())
	{
		if (in_array($dbCore->Record["departmentid"], $departmentidlist))
		{
			continue;
		}
		
		// We only add those departments that have their type set to livesupport module
		if ($dbCore->Record["departmentmodule"] == MODULE_LIVESUPPORT)
		{
			$departmentidlist[] = $dbCore->Record["departmentid"];
		}
	}

	return $departmentidlist;
}

/**
* Deletes the given chat objects
*/
function deleteChatObjects($chatobjectidlist)
{
	global $dbCore, $_SWIFT;

	// First get the departments for this staff
	$departmentidlist = getChatDepartments();

	// Now get the departmentid from chat objects
	$delchatobjectidlist = array();
	$dbCore->query("SELECT `chatobjectid`, `departmentid` FROM `". TABLE_PREFIX ."chatobjects` WHERE `chatobjectid` IN (". buildIN($chatobjectidlist) .");");
	while ($dbCore->nextRecord())
	{
		if (in_array($dbCore->Record["departmentid"], $departmentidlist))
		{
			$delchatobjectidlist[] = $dbCore->Record["chatobjectid"];
		}
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."chatobjects` WHERE `chatobjectid` IN (". buildIN($delchatobjectidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."chatdata` WHERE `chatobjectid` IN (". buildIN($delchatobjectidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."chatchilds` WHERE `chatobjectid` IN (". buildIN($delchatobjectidlist) .");");

	return true;
}
?>